package caidan.net;

import caidan.MainApp;
import caidan.SetFoodDataDialog;
import caidan.res.Resource;
import jinyilw.common.Operator;
import jinyilw.common.TextTools;
import jinyilw.common.app.AppConfig;
import jinyilw.common.file.FileTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.NativeTools;
import jinyilw.swing.SwingTools;
import jinyilw.swing.graphics.GraphicsTools;
import jinyilw.swing.image.ImageTools;
import jinyilw.swing.image.ThumbMaker;
import jinyilw.swing.rlaf.utils.RBorderFactory;
import jinyilw.swing.visuals.Jcomp.AddFileComp;
import jinyilw.swing.visuals.Jcomp.AddImageComp;
import jinyilw.swing.visuals.Jcomp.JActImageComp;
import jinyilw.swing.visuals.Jcomp.JImageComp;
import jinyilw.swing.visuals.Rcomp.ROptionPane;
import jinyilw.swing.visuals.border.RTitledBorder;
import jinyilw.swing.visuals.border.RichTitleBorder;
import jinyilw.swing.visuals.comp.RoundIconButton;
import jinyilw.swing.visuals.layout.VEFlowLayout;
import jinyilw.swing.visuals.window.HintWindow;
import jinyilw.tools.net.dev.CommandPacket;
import jinyilw.tools.net.file.FileTransfer;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractNewFoodPanel extends JPanel
{
	private static final long serialVersionUID = -3718194588048482704L;
	private static Dimension fooDimension = new Dimension(150, 120);

	protected File clientDataFolder;
	protected File netNewFoodFolder;
	protected File localNewFoodFolder;
	protected File newFoodDataFile;
	protected JPanel contentPanel, bottomPanel;
	protected JButton addButton;
	protected ArrayList<NewFoodLabel> newFoodLabels, localLabels;
	private SetFoodDataDialog setFoodDataDialog;
	private NewFoodView newFoodView;

	public AbstractNewFoodPanel()
	{
		super(new BorderLayout());
		initDataFolder();
		init();
		newFoodLabels = new ArrayList<NewFoodLabel>();
		localLabels = new ArrayList<NewFoodLabel>();
	}

	private void init()
	{
		createGUI();
		initLayout();
		initListeners();
	}

	private void initDataFolder()
	{
		clientDataFolder = FileTools.createDirectory(
				AppConfig.getAppDataFolder(), getDataFolderPath());
		netNewFoodFolder = FileTools.createDirectory(clientDataFolder,
				"newFoodInNet");
		localNewFoodFolder = FileTools.createDirectory(clientDataFolder,
				"newFood");
		newFoodDataFile = new File(clientDataFolder, getNewFoodFolderPath());
	}
	private void createGUI()
	{
		contentPanel = new JPanel();
		bottomPanel = new JPanel();
		addButton = new JButton("添加新菜推荐", Resource.addIcon);
		// freshButton = new JButton("刷新或重连网络", Resource.freshIcon);
		setBorder(new RTitledBorder(null, "新菜推荐", TitledBorder.CENTER,
				TitledBorder.BELOW_TOP, FontTools.getCustomFont(18),
				Color.cyan));
	}

	private void initLayout()
	{
		contentPanel.setLayout(new VEFlowLayout());
		add(new JScrollPane(contentPanel), BorderLayout.CENTER);
		add(bottomPanel, BorderLayout.SOUTH);
		bottomPanel.add(addButton);
		// bottomPanel.add(freshButton);
	}

	private void initListeners()
	{
		addButton.addActionListener((ActionEvent e) ->
		{
			showNewFoodDialog();
		});
	}

	private String[] options = {"确定", "取消"};
	private Object[] newFoodDialogObjects;
	private AddImageComp addImageComp;
	private AddFileComp addFileComp;
	private JTextField nameField;
	private JTextArea textArea;
	private int optionResult = -1;
	private void showNewFoodDialog()
	{
		if (newFoodDialogObjects == null)
		{
			newFoodDialogObjects = new Object[9];
			newFoodDialogObjects[0] = "添加新菜图片：";
			newFoodDialogObjects[1] = addImageComp = createAddImageComp();
			newFoodDialogObjects[2] = "输入新菜名称：";
			newFoodDialogObjects[3] = nameField = new JTextField(20);
			newFoodDialogObjects[4] = "\n输入制作方法：";
			newFoodDialogObjects[5] = createTextScrollPane();
			newFoodDialogObjects[6] = "或选择制作文件：";
			newFoodDialogObjects[7] = addFileComp = createAddFileComp();
			newFoodDialogObjects[8] = "\n";
		}
		if (optionResult != -1)
		{
			addImageComp.reset();
			nameField.setText("");
			textArea.setText("");
			addFileComp.reset();
		}
		optionResult = ROptionPane.showOptionDialog(this, newFoodDialogObjects,
				"添加新菜", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
				Resource.newFoodIcon, options, null);
		if (optionResult == 0)
		{
			String name = nameField.getText();
			String text = textArea.getText();
			File imgFile = addImageComp.getImageFile();
			File chooseFile = addFileComp.getFile();
			if (TextTools.isBlank(name))
			{
				ROptionPane.showMessageDialog(this, "菜单名称错误！请重新输入。");
				optionResult = -1;
				showNewFoodDialog();
			} else if (imgFile == null)
			{
				ROptionPane.showMessageDialog(this, "新菜图片为空！请添加一张图片。");
				optionResult = -1;
				showNewFoodDialog();
			} else
			{

				long longData = System.currentTimeMillis();
				double doubleData = Math.random();
				String makeFileName = name + longData;
				String imageFileName = makeFileName + "_img."
						+ TextTools.getFileExt(imgFile.getPath());
				String dataFileName = makeFileName + "_data";
				File imageFile = new File(localNewFoodFolder, imageFileName);
				FileTools.copyFile(imgFile, imageFile);
				File makeFile = null;
				if (chooseFile != null)
				{
					makeFileName += "."
							+ TextTools.getFileExt(chooseFile.getPath());
					makeFile = new File(localNewFoodFolder, makeFileName);
					FileTools.copyFile(chooseFile, makeFile);
				}
				File dataFile = new File(localNewFoodFolder, dataFileName);
				getSetFoodDataDialog();
				setFoodDataDialog.updateContent(addImageComp.getImage(), name,
						dataFile);
				setFoodDataDialog.setVisible(true);
				String settingName = setFoodDataDialog.getSettingName();
				if (settingName != null)
					name = settingName;

				CommandPacket foodData = new CommandPacket();
				foodData.setIntData(AppConfig.appVersion);
				foodData.setStringData(name);
				foodData.setLongData(longData);
				foodData.setDoubleData(doubleData);
				NewFoodLabel label = new NewFoodLabel(foodData);
				foodData.setStringArr(new String[]{imageFileName, makeFileName,
						dataFileName});
				foodData.setByteArr(MainApp.userName.getBytes());
				label.setFiles(new File[]{imageFile, makeFile, dataFile});
				if (chooseFile == null)
					label.setText(text);
				addNewFoodLabel(label);
				//				if (optionResult == 0)
				//					sendNewFoodToNet(label);
			}
		}
	}

	private AddImageComp createAddImageComp()
	{
		AddImageComp addImageComp = new AddImageComp();
		addImageComp.setPreferredSize(new Dimension(450, 260));
		addImageComp.setBorder(RBorderFactory
				.createSimpleRoundBorder(GraphicsTools.getDottedStroke()));
		addImageComp.addDropOperater((Object o) ->
		{
			if (o instanceof List)
			{
				File f = (File) ((List) o).get(0);
				addImageComp.setImageFile(f);
				if (!addImageComp.isBlank())
					addImageComp.removeSubComps();
			}
		});
		addImageComp.addOperater(new Operator()
		{
			FileDialog fileDialog = null;
			@Override
			public void operate(Object o)
			{
				if (fileDialog == null)
				{
					fileDialog = NativeTools.createNativeFileDialog(
							addFileComp.getParent(), "选择新菜单图片", FileDialog.LOAD,
							ImageTools.pictureExts);
				}
				fileDialog.setVisible(true);
				File[] files = fileDialog.getFiles();
				if (files.length > 0)
					addImageComp.setImageFile(files[0]);
				if (!addImageComp.isBlank())
					addImageComp.removeSubComps();
			}
		});
		return addImageComp;
	}

	private JScrollPane createTextScrollPane()
	{
		textArea = new JTextArea(8, 25);
		textArea.setLineWrap(true);
		textArea.setFont(FontTools.defaultFont);
		JScrollPane scrollPane = new JScrollPane(textArea);
		return scrollPane;
	}

	private AddFileComp createAddFileComp()
	{
		AddFileComp addFileComp = new AddFileComp();
		addFileComp.setPreferredSize(new Dimension(450, 30));
		addFileComp.addClickOperater(new Operator()
		{
			FileDialog fileDialog = null;
			@Override
			public void operate(Object o)
			{
				if (fileDialog == null)
				{
					fileDialog = NativeTools.createNativeFileDialog(
							addFileComp.getParent(), "选择制作文件", FileDialog.LOAD);
				}
				fileDialog.setVisible(true);
				File[] files = fileDialog.getFiles();
				if (files.length > 0)
				{
					if (files[0].length() > 3145728)
					{
						ROptionPane.showMessageDialog(addFileComp.getParent(),
								"文件不得大于3M，请压缩后重新选择", "选择制作文件");
						return;
					}
					addFileComp.setFile(files[0]);
				}
			}
		});
		addFileComp.addDropOperater((Object o) ->
		{
			if (o instanceof List)
			{
				File f = (File) ((List) o).get(0);
				if (f.length() > 3145728)
				{
					ROptionPane.showMessageDialog(addFileComp.getParent(),
							"文件不得大于3M，请压缩后重新选择", "选择制作文件");
					return;
				}
				addFileComp.setFile(f);
			}
		});
		return addFileComp;
	}

	//	public void addNewFoodFromNet(CommandPacket foodData)
	//	{
	//		addNewFoodLabel(new NewFoodLabel(foodData, true));
	//	}
	//
	//	public void netDeleteNewFood(CommandPacket foodData)
	//	{
	//		NewFoodLabel label = getNetLabel(foodData.getLongData(),
	//				foodData.getDoubleData());
	//		if (label != null)
	//		{
	//			removeNewFoodLabel(label);
	//			label.forceDeleteFile();
	//			return;
	//		}
	//		String[] fileNames = foodData.getStringArr();
	//		File imageFile = new File(netNewFoodForder, fileNames[0]);
	//		imageFile.delete();
	//		ThumbMaker.getDefaultThumbFile(imageFile).delete();
	//		new File(netNewFoodForder, fileNames[1]).delete();
	//	}

	//	public void getFiles(CommandPacket foodData)
	//	{
	//		NewFoodLabel label = getNetLabel(foodData.getLongData(),
	//				foodData.getDoubleData());
	//		if (label == null)
	//			return;
	//		String ip = clientTools.getServerIP();
	//		int port = foodData.getIntData();
	//		try
	//		{
	//			Thread.sleep(200);
	//		} catch (InterruptedException e)
	//		{
	//			e.printStackTrace();
	//		}
	//		File[] files = TransferTools.receiveFilesFromNetTemporarily(ip, port,
	//				netNewFoodForder);
	//		if (files != null)
	//		{
	//			label.setImageFile(files[0]);
	//			label.setMakeFile(files[1]);
	//		}
	//	}

	//	private NewFoodLabel getNetLabel(long longData, double doubleData)
	//	{
	//		for (NewFoodLabel label : newFoodLabels)
	//			if (longData == label.foodData.getLongData()
	//					&& doubleData == label.foodData.getDoubleData())
	//				return label;
	//		return null;
	//	}
	//
	//	private void initFromFile()
	//	{
	//
	//	}

	public SetFoodDataDialog getSetFoodDataDialog()
	{
		if (setFoodDataDialog == null)
			setFoodDataDialog = new SetFoodDataDialog(
					SwingUtilities.getWindowAncestor(this),
					ModalityType.APPLICATION_MODAL);
		return setFoodDataDialog;
	}

	public NewFoodView getNewFoodView()
	{
		if (newFoodView == null)
			newFoodView = new NewFoodView(
					SwingUtilities.getWindowAncestor(this), this);
		return newFoodView;
	}

	public void writeToFile()
	{
		try (DataOutputStream dos = new DataOutputStream(
				new FileOutputStream(newFoodDataFile)))
		{
			dos.writeInt(AppConfig.appVersion);
			dos.writeInt(newFoodLabels.size() + localLabels.size());
			for (NewFoodLabel label : newFoodLabels)
				label.writeData(dos);
			for (NewFoodLabel label : localLabels)
				label.writeData(dos);
			dos.close();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void readFromFile()
	{
		if (newFoodDataFile == null || !newFoodDataFile.isFile())
			return;
		try (DataInputStream dis = new DataInputStream(
				new FileInputStream(newFoodDataFile)))
		{
			NewFoodData newFoodData = new NewFoodData();
			NewFoodLabel label;
			int appVersion = dis.readInt();
			int length = dis.readInt();
			for (int j = 0; j < length; j++)
			{
				newFoodData.readData(dis, appVersion);
				if (isClient() && isConnected() && newFoodData.isNet)
					continue;
				label = new NewFoodLabel(newFoodData.createFoodData(),
						newFoodData.isNet, newFoodData.isSend);
				if (newFoodData.isNet)
					newFoodLabels.add(label);
				else
					localLabels.add(label);
			}
			dis.close();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void removeNewFoodLabel(NewFoodLabel label)
	{
		if (label == null)
			return;
		contentPanel.remove(label);
		revalidate();
		repaint();
		if (label.isNet)
			newFoodLabels.remove(label);
		else
			localLabels.remove(label);
	}

	public void addNewFoodLabel(NewFoodLabel nLabel)
	{
		if (nLabel == null)
			return;
		if (nLabel.isNet)
		{
			contentPanel.add(nLabel, newFoodLabels.size());
			newFoodLabels.add(nLabel);
		} else
		{
			contentPanel.add(nLabel);
			localLabels.add(nLabel);
		}
		revalidate();
		repaint();
	}

	public abstract FileTransfer getFileTransfer();

	public abstract boolean sendCommandToNet(CommandPacket data, int command);

	public abstract boolean isConnected();

	public abstract boolean isClient();

	public abstract String getDataFolderPath();

	public abstract String getNewFoodFolderPath();

	class NewFoodLabel extends JImageComp implements NewFood
	{
		private static final long serialVersionUID = 6135927525395642839L;
		CommandPacket foodData;
		boolean isNet, isSend;
		private JActImageComp sendLabel;
		private RichTitleBorder titleBorder = new RichTitleBorder(null,
				FontTools.defaultFont);
		private RoundIconButton closeButton = new RoundIconButton(
				Resource.closeIcon);
		private ThumbMaker thumbMaker = new ThumbMaker(140, 100, 12);
		private String text;
		private boolean isText;
		private File[] files;
		private String[] fileNames;

		public NewFoodLabel(CommandPacket foodData)
		{
			this(foodData, false);
		}

		public NewFoodLabel(CommandPacket foodData, boolean isNet)
		{
			this(foodData, isNet, false);
		}

		public NewFoodLabel(CommandPacket foodData, boolean isNet,
				boolean isSend)
		{
			setBorder(titleBorder);
			closeButton.addClickOperator((Object o) ->
			{
				deleteNewFood();
				deleteFile();
			});
			SwingTools.addTitleComponents(this, closeButton);
			setLabel(foodData, isNet, isSend);

			addClickOperater((Object o) ->
			{
				NewFoodView view = getNewFoodView();
				view.setNewFood(this);
				if (!view.isVisible())
					view.setVisible(true);
			});
		}

		void update(CommandPacket foodData)
		{
			int version = foodData.getIntData();
			if (version < 305)
			{
				foodData.setIntData(AppConfig.appVersion);
				String userName = foodData.getStringArr()[2];
				foodData.setByteArr(userName.getBytes());
				foodData.getStringArr()[2] = foodData.getStringData()
						+ foodData.getLongData() + "_data";
			}
		}

		public void setLabel(CommandPacket foodData, boolean isNet,
				boolean isSend)
		{
			update(foodData);
			this.foodData = foodData;
			this.isNet = isNet;
			this.isSend = isSend;
			fileNames = foodData.getStringArr();
			if (fileNames != null)
				setFiles(foodData);

			updateTitle();
			if (isNet)
			{
				if (sendLabel != null)
					remove(sendLabel);
			} else
			{
				if (sendLabel == null)
				{
					sendLabel = new JActImageComp(Resource.sendIcon);
					sendLabel.addClickOperater((Object o) ->
					{
						sendNewFoodToNet();
					});
				}
				SwingTools.addTitleComponents(this, sendLabel);
				if (isSend)
				{
					sendLabel.setToolTipText("已发送");
					sendLabel.setEnabled(false);
				} else
				{
					sendLabel.setToolTipText("发送到网络");
					sendLabel.setEnabled(true);
				}
			}
		}

		public void updateTitle()
		{
			String title = foodData.getStringData();
			if (isClient())
			{
				if (isNet)
				{
					title += "-网络";
					if (!isConnected())
						title += "(缓存)";
				} else
					title += "-本地";
			} else
				title += isSend ? "-已推送" : "-未推送";
			titleBorder.setTitle(title);
		}

		private void setFiles(CommandPacket foodData)
		{
			File currentFolder = isNet ? netNewFoodFolder : localNewFoodFolder;
			if (fileNames[0] == null)
				return;
			isText = fileNames[1].indexOf(".") == -1;
			File imageFile = new File(currentFolder, fileNames[0]);
			if (imageFile.isFile())
			{
				File makeFile = new File(currentFolder, fileNames[1]);
				File dataFile = new File(currentFolder, fileNames[2]);
				files = new File[]{imageFile, makeFile, dataFile};
				thumbMaker.setFile(imageFile);
				setImage(thumbMaker.getThumbnail());
			} else if (isClient()
					&& foodData.getCommandID() == Command.S.sendNewFoodToNet)
				new Thread(() ->
				{
					getNetFiles();
				}).start();
		}

		void getNetFiles()
		{
			File imageFile = new File(netNewFoodFolder, fileNames[0]);
			if (imageFile.isFile())
				return;
			FileTransfer fileTransfer = getFileTransfer();
			if (fileTransfer == null)
				return;
			File[] files = fileTransfer.getFilesAndWait(10000, fileNames);
			setFiles(files);
		}

		void sendNewFoodToNet()
		{
			if (isClient())
			{
				if (sendCommandToNet(foodData, Command.C.sendNewFoodToNet))
				{
					isSend = true;
					sendLabel.setEnabled(false);
					HintWindow.showHint("已发送到网络！", getParent());
					sendLabel.setToolTipText("已发送");
					FileTransfer fileTransfer = getFileTransfer();
					if (fileTransfer != null)
						try
						{
							fileTransfer.sendFiles(files);
						} catch (IOException e)
						{
							e.printStackTrace();
						}
				} else
					HintWindow.showHint("发送失败！", getParent());
			} else
			{
				contentPanel.setComponentZOrder(this, newFoodLabels.size());
				contentPanel.revalidate();
				contentPanel.getParent().repaint();
				localLabels.remove(this);
				newFoodLabels.add(this);
				File imageFile = getImageFile();
				if (imageFile != null && imageFile.isFile())
				{
					fileNames = foodData.getStringArr();
					File newImageFile = new File(netNewFoodFolder,
							fileNames[0]);
					FileTools.move(imageFile, newImageFile);
					File newMakeFile = null;
					File makeFile = getMakeFile();
					if (makeFile != null && makeFile.isFile())
					{
						newMakeFile = new File(netNewFoodFolder, fileNames[1]);
						FileTools.move(makeFile, newMakeFile);
					}
					File newDataFile = null;
					File dataFile = getDataFile();
					if (dataFile != null && dataFile.isFile())
					{
						newDataFile = new File(netNewFoodFolder, fileNames[2]);
						FileTools.move(dataFile, newDataFile);
					}
					deleteFile();
					setImageFile(newImageFile);
					setMakeFile(newMakeFile);
					setDataFile(newDataFile);
				}
				sendCommandToNet(foodData, Command.S.sendNewFoodToNet);
				isSend = true;
				isNet = true;
				sendLabel.setEnabled(false);
				titleBorder.setTitle(foodData.getStringData() + "-已推送");
				HintWindow.showHint("已发送到网络！", getParent());
				sendLabel.setToolTipText("已发送");
			}
		}

		void deleteNewFood()
		{
			removeNewFoodLabel(this);
			if (!isClient() && isNet)
				sendCommandToNet(foodData, Command.S.deleteNewFoodInNet);
		}

		void setFiles(File[] files)
		{
			if (files == null || files.length < 1)
				return;
			this.files = files;
			thumbMaker.setFile(files[0]);
			setImage(thumbMaker.getThumbnail());
		}

		public void deleteFile()
		{
			if (!isClient() || !isNet)
				forceDeleteFile();
		}

		void forceDeleteFile()
		{
			thumbMaker.delete();
			if (files != null)
				for (int i = 1; i < files.length; i++)
					if (files[i] != null)
						files[i].delete();
		}

		public void setImageFile(File file)
		{
			if (files == null)
				files = new File[3];
			files[0] = file;
			thumbMaker.setFile(file);
			setImage(thumbMaker.getThumbnail());
		}

		public void setMakeFile(File file)
		{
			if (files == null)
				files = new File[3];
			files[1] = file;
		}

		public void setDataFile(File file)
		{
			if (files == null)
				files = new File[3];
			files[2] = file;
		}

		public void setText(String text)
		{
			if (this.text == text || files == null || files.length < 2)
				return;
			this.text = text;
			isText = true;
			if (files[1] == null)
				files[1] = new File(localNewFoodFolder,
						foodData.getStringArr()[1]);
			TextTools.writeUTF8TxtFile(files[1], text);
		}

		public void writeData(DataOutputStream dos)
		{
			try
			{
				dos.writeBoolean(isNet);
				dos.writeBoolean(isSend);
				dos.writeInt(foodData.getIntData());
				dos.writeLong(foodData.getLongData());
				dos.writeDouble(foodData.getDoubleData());
				dos.writeUTF(foodData.getStringData());
				dos.writeInt(foodData.getStringArr().length);
				for (String s : foodData.getStringArr())
					dos.writeUTF(s);
				dos.writeInt(foodData.getByteArr().length);
				for (byte b : foodData.getByteArr())
					dos.writeByte(b);
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		@Override
		public Dimension getPreferredSize()
		{
			return fooDimension;
		}

		@Override
		public void setFoodName(String name)
		{
			if (name != null)
				foodData.setStringData(name);
		}

		@Override
		public String getFoodName()
		{
			return foodData.getStringData();
		}

		@Override
		public File getImageFile()
		{
			if (files != null && files.length > 0)
				return files[0];
			return null;
		}

		@Override
		public File getMakeFile()
		{
			if (files != null && files.length > 1)
				return files[1];
			return null;
		}

		@Override
		public File getDataFile()
		{
			if (files != null && files.length > 2)
				return files[2];
			return null;
		}

		@Override
		public boolean isText()
		{
			return isText;
		}

		@Override
		public String getText()
		{
			if (text == null && isText)
				text = TextTools.readUTF8TxtFile(getMakeFile());
			return text;
		}

		@Override
		public String getUser()
		{
			return new String(foodData.getByteArr());
		}

		@Override
		public Image getImage()
		{
			return thumbMaker.getOriginalImage();
		}
	}

	public static class NewFoodData
	{
		boolean isNet, isSend;
		int version;
		long longData;
		double doubleData;
		String name;
		String[] stringArr;
		byte[] byteArr;

		public void readData(DataInputStream dis, int appVersion)
				throws IOException
		{
			if (appVersion < 305)
			{
				isNet = dis.readBoolean();
				isSend = dis.readBoolean();
				longData = dis.readLong();
				doubleData = dis.readDouble();
				name = dis.readUTF();
				stringArr = new String[3];
				for (int i = 0; i < stringArr.length; i++)
					stringArr[i] = dis.readUTF();
			} else
			{
				isNet = dis.readBoolean();
				isSend = dis.readBoolean();
				version = dis.readInt();
				longData = dis.readLong();
				doubleData = dis.readDouble();
				name = dis.readUTF();
				int length = dis.readInt();
				stringArr = new String[length];
				for (int i = 0; i < stringArr.length; i++)
					stringArr[i] = dis.readUTF();
				length = dis.readInt();
				byteArr = new byte[length];
				for (int i = 0; i < byteArr.length; i++)
					byteArr[i] = dis.readByte();
			}
		}

		public CommandPacket createFoodData()
		{
			CommandPacket data = new CommandPacket();
			data.setIntData(version);
			data.setLongData(longData);
			data.setDoubleData(doubleData);
			data.setStringData(name);
			data.setStringArr(stringArr);
			data.setByteArr(byteArr);
			return data;
		}
	}
}
